{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>添加优惠券</h5>
                </div>
                <div class="ibox-content layui-form">
                    <div class="layui-row layui-col-space10 layui-form-item ">

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_name" lay-verify="required" placeholder="名称" class="layui-input ">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">面额：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_price" lay-verify="required" placeholder="面额" class="layui-input ">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 form-group">
                            <label class="layui-form-label">获取途径：</label>
                            <div class="layui-input-block">
                                <select name="coupon_type" class="coupon_type" lay-filter="coupon_type">
                                    <option value="1" selected>闯关</option>
                                    <option value="2" >对战</option>
                                    <option value="3" >段位升级</option>
                                    <option value="4" >勋章</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 form-group">
                            <label class="layui-form-label">使用范围：</label>
                            <div class="layui-input-block">
                                <select name="coupon_use" class="coupon_use" lay-filter="role">
                                    <option value="1" selected>现金商品</option>
                                    <option value="2" >金币商品</option>
                                    <option value="3" >现金类目</option>
                                    <option value="4" >金币类目</option>
                                    <option value="5" >全部现金图书</option>
                                    <option value="6" >全部金币图书</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8" id="unbooks_id">
                            <label class="layui-form-label">选择商品：</label>
                            <div class="layui-input-block">
                                <select name="unbook_id"  xm-select="select1"  xm-select-search="" xm-select-radio="" xm-select-search-type="dl">
                                    <option value="" >请选择</option>
                                    {foreach name="unbooks" item="v"}
                                    <option value="{$v.good_id}" >{$v.good_name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8" style="display: none" id="books_id">
                            <label class="layui-form-label">选择商品：</label>
                            <div class="layui-input-block">
                                <select name="book_id"  xm-select="select2" xm-select-search="" xm-select-radio="" xm-select-search-type="dl">
                                    <option value="" >请选择</option>
                                    {foreach name="books" item="v"}
                                    <option value="{$v.good_id}">{$v.good_name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8" style="display: none" id="money_id">
                            <label class="layui-form-label">选择类目：</label>
                            <div class="layui-input-block">
                                <select name="unassortment_id"  xm-select="select3" xm-select-search="" xm-select-radio="" xm-select-search-type="dl">
                                    <option value="" >请选择</option>
                                    {foreach name="money" item="v"}
                                    <option value="{$v.assortment_id}">{$v.assortment_name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8" style="display: none" id="gold_id">
                            <label class="layui-form-label">选择类目：</label>
                            <div class="layui-input-block">
                                <select name="assortment_id"  xm-select="select4" xm-select-search="" xm-select-radio="" xm-select-search-type="dl">
                                    <option value="" >请选择</option>
                                    {foreach name="gold" item="v"}
                                    <option value="{$v.assortment_id}">{$v.assortment_name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">使用说明：</label>
                            <div class="layui-input-block">
                                <textarea name="coupon_content" lay-verify="required" class="layui-input " style="height:80px"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 coupon_num" style="display:none">
                            <label class="layui-form-label">设置场数：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_num"  placeholder="对战连胜场数(2,3,4,5,6)" class="layui-input ">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">生效时间：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="coupon_start" lay-verify="required" placeholder="生效时间" class="layui-input test-item">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">失效时间：</label>
                            <div class="layui-input-inline">
                                <input type="text"  name="coupon_end" lay-verify="required" placeholder="失效时间" class="layui-input test-item">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">添加
                                </button>
                                <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">关闭</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    layui.use(['form','upload','layedit','laydate'], function() {
        var form = layui.form;
        form.verify({
            editor:function(value,item){
                if(editor.txt.html() == '<p><br></p>'){
                    return '请填写内容';
                }
            }
        });
        form.on('submit(component-form-element)', function (data) {
//            coupon_type = $(".coupon_type").val();
//            coupon_num = $(".coupon_num").val();
//            if(coupon_type ==2){
//                if(coupon_num ==''||coupon_num== null){
//                    layer.msg("对战连胜场数不能为空", {icon: 1, time: 1500, shade: 0.1}, function (index) {
//                    });
//                    return false;
//                }
//            }

            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            $.ajax({
                url:"{:url('couponAdd')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        wk.success(res.msg,"{:url('Coupon/index')}")
                    } else {
                        wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\');');
                    }
                }
            })
        });
        form.on('select(role)', function(data){
            if(data.value == 2){
                $("#unbooks_id").hide();
                $("#books_id").show();
                $("#money_id").hide();
                $("#gold_id").hide();
            }else if(data.value == 3){
                $("#unbooks_id").hide();
                $("#books_id").hide();
                $("#money_id").show();
                $("#gold_id").hide();
            }else if(data.value == 4){
                $("#unbooks_id").hide();
                $("#books_id").hide();
                $("#money_id").hide();
                $("#gold_id").show();
            }else if(data.value ==1){
                $("#unbooks_id").show();
                $("#books_id").hide();
                $("#money_id").hide();
                $("#gold_id").hide();
            }else{
                $("#unbooks_id").hide();
                $("#books_id").hide();
                $("#money_id").hide();
                $("#gold_id").hide();
            }
        });

        form.on('select(coupon_type)', function(data){
            if(data.value == 2){
               $(".coupon_num").show();
            }else{
                $(".coupon_num").hide();
            }
        });
        var laydate = layui.laydate;
        //同时绑定多个
        lay('.test-item').each(function(){
            laydate.render({
                elem: this
                ,format:'yyyy-MM-dd HH:mm:ss'
                ,type:'datetime'
                ,trigger: 'click'
            });
        });

    });
</script>

</body>
</html>
